package com.epfresh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.CenterListActivity;
import com.epfresh.activity.MoreEvaluateActivity;
import com.epfresh.activity.WebActivity;
import com.epfresh.api.utils.T;
import com.epfresh.bean.StoreDetail;
import com.epfresh.constant.Constant;
import com.epfresh.global.BaseFragment;
import com.epfresh.utils.CommonUtils;
import com.epfresh.views.stick.OnStickyListener;
import com.epfresh.views.stick.StickyScrollView;

/**
 * Created by wzh on 16-11-11.
 */

public class DcStoreInfoFragment extends BaseFragment {

//    UNCERTIFIED(0, "未认证"),
//    CERTIFYING(1, "认证中"),
//    HAS_CERTIFIED(2, "已认证"),
//    REJECTED(3, "认证失败");
private TextView tvMarket;
    private TextView tvAddress;
    private TextView tvPhone;
    private TextView tvPayment;
    private TextView tvDiscount;
    private TextView tvDelivery;
    //private TextView tvCert;
    private TextView tvRate;
    private TextView tvAmount;
    private TextView tvAcceptTime;
    private TextView tvDiscountLevel;
    private TextView tvDiscountDetail;
    private TextView tvCategory;
    private TextView tvDetail;
    private View llRate;
    private View vDiscount;


    private StoreDetail storeDetail;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.view_store_info, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        initView(view);
      
    }


    private void initView( View v) {


        vDiscount = v.findViewById(R.id.rl_discount);
        tvMarket = (TextView) v.findViewById(R.id.tv_market);
        tvAddress = (TextView) v.findViewById(R.id.tv_address);
        tvPhone = (TextView) v.findViewById(R.id.tv_phone);
        tvPayment = (TextView) v.findViewById(R.id.tv_payment);
        tvDiscount = (TextView) v.findViewById(R.id.tv_discount);
        tvDelivery = (TextView) v.findViewById(R.id.tv_delivery);
        //tvCert = (TextView) v.findViewById(R.id.tv_cert_status);
        tvRate = (TextView) v.findViewById(R.id.tv_rate);
        tvAmount = (TextView) v.findViewById(R.id.tv_amount);
        tvAcceptTime = (TextView) v.findViewById(R.id.tv_accept_time);
        llRate = v.findViewById(R.id.ll_comment);
        v.findViewById(R.id.iv_rate).setOnClickListener(this);
        v.findViewById(R.id.tv_rate).setOnClickListener(this);

//        llRate.setOnClickListener(this);

        v.findViewById(R.id.ll_phone).setOnClickListener(this);


        tvDiscountLevel = (TextView) v.findViewById(R.id.tv_discount_level);
        tvDiscountDetail = (TextView) v.findViewById(R.id.tv_discount_detail);
        tvCategory = (TextView) v.findViewById(R.id.tv_category);
        tvDetail = (TextView) v.findViewById(R.id.tv_detail);

        updateStoreStatus();
    }



    public void updateData(StoreDetail storeDetail) {
        this.storeDetail = storeDetail;
        updateStoreStatus();

    }


    public void updateStoreStatus() {
        if (tvMarket != null) {
            if (storeDetail != null) {
                tvDiscountLevel.setVisibility(View.VISIBLE);
                tvDiscountDetail.setVisibility(View.VISIBLE);
                tvDiscountDetail.setText(storeDetail.getDiscountInfo());
                if(storeDetail.getDiscountInfo()==null||"".equals(storeDetail.getDiscountInfo())){
                    vDiscount.setVisibility(View.GONE);
                }
                tvMarket.setText(storeDetail.getMarketName());
                tvAddress.setText(storeDetail.getAddress());
                tvPhone.setText(storeDetail.getManagerPhone());
                tvPayment.setText(storeDetail.getPaymentInfo());
                tvDiscount.setText(storeDetail.getDiscount());
                tvDelivery.setText(storeDetail.getDeliveryInfo());
                //tvCert.setText(storeDetail.getCert());
                tvAmount.setText(storeDetail.getOrderCount() + "单");
                tvRate.setText(storeDetail.getGoodCommentsRate() + "好评率 >");
                String businessTime = storeDetail.getBusinessTime();
                if (businessTime == null || "".equals(businessTime)){
                    tvAcceptTime.setText("00:00-24:00");
                }else {

                    tvAcceptTime.setText(businessTime);
                }
                tvCategory.setText(storeDetail.getMainBusinessCategoryNames());
                tvDetail.setText(storeDetail.getDescription());
            } else {
                tvMarket.setText("");
                tvAddress.setText("");
                tvPhone.setText("");
                tvPayment.setText("");
                tvDiscount.setText("");
                tvDelivery.setText("");
                //tvCert.setText("");
                tvAmount.setText("");
                tvRate.setText("");
                tvDiscountLevel.setVisibility(View.GONE);
                tvDiscountDetail.setVisibility(View.GONE);
                tvDiscountLevel.setText("");
                tvDiscountDetail.setText("");
                tvCategory.setText("");
                tvDetail.setText("");
            }
        }
    }

    @Override
    public String getBaseTag() {
        return "";
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_more_dc:
//                intoMoreDc();

                break;
            case R.id.iv_rate:
            case R.id.tv_rate:
                intoComment();

                break;
            case R.id.ll_phone:
                String managerPhone = storeDetail.getManagerPhone();
                if (managerPhone != null && !"".equals(managerPhone))
                    CommonUtils.intoPhone(getActivity(), managerPhone);

                break;
        }
    }

    private void intoComment() {

        if(storeDetail==null){
            T.toast("数据已过期，请重载应用");
        }
//        Intent more = new Intent(getContext(), MoreEvaluateActivity.class);
//        more.putExtra("storeId", storeDetail.getId());
//        if(storeDetail!=null&&storeDetail.getGoodCommentsRate()!=null&&storeDetail.getGoodCommentsRate().length()>1){
//            String rate = storeDetail.getGoodCommentsRate().substring(0,storeDetail.getGoodCommentsRate().length()-1);
//            more.putExtra("evaluate_percent",rate);
//        }else
//            more.putExtra("evaluate_percent","0");
//        startActivity(more);

        Intent h5Intent = new Intent(getAppActivity(), WebActivity.class);
        h5Intent.putExtra(Constant.KEY_WEB_URL, com.epfresh.api.constant.Constant.URL_GOODS_DETAIL_COMMENT + "storeId=" + storeDetail.getId());
        h5Intent.putExtra(Constant.KEY_WEB_TITLE,"评价");
        startActivity(h5Intent);

/*        Intent more = new Intent(getActivity(), MoreEvaluateActivity.class);
        if (storeDetail == null) {
            T.toast("数据已过期，请重载应用");
        }
        more.putExtra("evaluate_percent", storeDetail.getGoodCommentsRate());
        more.putExtra("storeId", storeDetail.getId());
        this.startActivity(more);*/
    }

}
